<?php
/*
	内容：商品发货提示设定类相关函数操作
	时间：2017.3.28
	庆豪
*/
class b2c_mdl_tips extends dbeav_model
{		

    function __construct(&$app)
    {
        $this->app = $app;
        $this->columns = array(
            'goods_name'=>array('label'=>app::get('b2c')->_('商品名称'),'width'=>200),
            );

        $this->schema = array(
                'default_in_list'=>array_keys($this->columns),
                'in_list'=>array_keys($this->columns),
                'idColumn'=>'gnotify_id',//这边最好改tips_id,后期改正
                'columns'=>$this->columns
            );
     }
	
    function get_schema(){
        return $this->schema;
    }

    //获取发货提示设置数据表的信息
    function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderby=null){
        $data = $this->get_tips_goods($filter,$offset,$limit,$orderby);
        return $data;
    }

    function count($filter=array()){
        $sql = 'SELECT count(*) AS _count FROM sdb_b2c_goods_tips';
        $row = kernel::database()->select($sql);
        return intval($row[0]['_count']);
    }

    //列表显示
    function get_tips_goods($filter,$offset=0,$limit=-1,$orderby=null)
    {
        $goods_tips = $this->app->model('goods_tips');
        $data = $goods_tips->getList('*');

        foreach($data as $tips_goods)
        {
            $result[] = array(
                'gnotify_id'=>$tips_goods['gnotify_id'],
                'goods_id'=>$tips_goods['goods_id'],
                'goods_name'=>$tips_goods['name']
                );
        }
        return $result;
    }
    
    
    //发货提示前台静态显示
    function getGoodsTips($goodsid)
    {
       
        $goodsTipModel=$this->app->model('goods_tips');
        $data=$goodsTipModel->getList('value',array('goods_id'=>$goodsid));
        $unserData=unserialize($data[0]['value']);
        $order_count=count($unserData);
        $row=0;
        
        foreach ($unserData as $key1 => $value1) {
            foreach ($value1 as $key2 => $value2) {
                $arr[$row][$key2]=$value2;
            }
            $row++;
            
        }
      
       /**该商品下的部分发货数量、发货状态、预发货数量**/
       $b2c_order_items=DB_PREFIX."b2c_order_items";
       $b2c_delivery_items=DB_PREFIX."b2c_delivery_items";
       $b2c_orders=DB_PREFIX."b2c_orders";
       $count_out=0;
       $flag=-1;
       //计算部分发货剩下的发货数量
       $check_goods_num_part="
        select 
        ($b2c_order_items.nums-$b2c_delivery_items.number) as result1
        from
        (
        $b2c_order_items
        inner join 
        $b2c_delivery_items
        on
        $b2c_delivery_items.order_item_id=$b2c_order_items.item_id
        )   
        inner join 
        $b2c_orders
        on 
        $b2c_orders.order_id=$b2c_order_items.order_id
        and 
        $b2c_orders.ship_status='2'
        and 
        $b2c_order_items.product_id='$productid'
       ";

       // 计算未发货的数量
       $check_goods_num="
       select 
       $b2c_order_items.nums as result
       from
       $b2c_order_items
       inner join 
       $b2c_orders
       on 
       $b2c_order_items.order_id=$b2c_orders.order_id
       and 
       $b2c_orders.ship_status='0'
       and 
       $b2c_order_items.product_id='$productid'
       ";
       $temp=kernel::database()->select($check_goods_num);
       $temp1=kernel::database()->select($check_goods_num_part);
       
      
       $count_out=$temp[0]['result']+$temp1[0]['result1'];//计算所以未发货的数量
       $all=$count_out+1;
       //echo $unserData[1]['max'];
       for($i=0;$i<$order_count;$i++)
       {
            if($all>=$arr[$i]['min']&&$all<=$arr[$i]['max'])
                $flag=$i;
            if($all>$arr[$i]['max'])
                $flag=$i;
       }
      
       
        if($flag==-1)
//            return "暂未设置发货信息";
            return "无";
        else
            return $arr[$flag]['title']; 
    }
    
    

}